package com.kaiyiweitong.tongxing.activity.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.GridLayout;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.adapter.HistorySearchAdapter;
import com.kaiyiweitong.tongxing.adapter.SearchResultAdapter;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.util.CommonUtil;
import com.kaiyiweitong.tongxing.util.MyTextUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/28 0028.
 */

public class SearchActivity extends BaseActivity {
    @Bind(R.id.search_content)
    EditText mSearchContent;
    @Bind(R.id.rv_history_search)
    RecyclerView mRvHistorySearch;
    @Bind(R.id.rv_search_result)
    RecyclerView mRvSearchResult;

    public static final String SEARCH_TEXT = "search_text";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        HistorySearchAdapter historySearchAdapter = new HistorySearchAdapter(this, mListener);
        mRvHistorySearch.setLayoutManager(new GridLayoutManager(this, 1, GridLayout.VERTICAL, false));
        mRvHistorySearch.setAdapter(historySearchAdapter);

        SearchResultAdapter searchResultAdapter = new SearchResultAdapter(this);
        mRvSearchResult.setLayoutManager(new GridLayoutManager(this, 1, GridLayout.VERTICAL, false));
        mRvSearchResult.setAdapter(searchResultAdapter);
        mRvSearchResult.setVisibility(View.INVISIBLE);

        Intent intent = getIntent();
        if (intent.getStringExtra(SEARCH_TEXT) != null && !intent.getStringExtra(SEARCH_TEXT).equals("")) {
            mSearchContent.setText(intent.getStringExtra(SEARCH_TEXT));
            search();
        }
    }

    private void search() {
        if (MyTextUtils.checkTextContent(mSearchContent).equals("")) {
            CommonUtil.toast(this, R.string.search_text_not_blank);
        } else {
            //TODO 请求服务器，获取服务器数据
        }
    }

    @OnClick(R.id.search)
    public void onViewClicked() {
        search();
    }

    private HistorySearchAdapter.OnClickListener mListener = new HistorySearchAdapter.OnClickListener() {
        @Override
        public void onClick(int position) {
            CommonUtil.toast(SearchActivity.this, "点击了第" + position + "个");
        }

        @Override
        public void onDelete(int position) {
            CommonUtil.toast(SearchActivity.this, "删除第" + position + "个");
        }
    };
}
